﻿using BenCaoXuan.Infrastructure.Attr;
using BenCaoXuan.Infrastructure.Dto;
using BenCaoXuan.Infrastructure.Dto.Middle.Response.Article;
using BenCaoXuan.Infrastructure.IOC;
using BenCaoXuan.Service.Interface.middle;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BenCaoXuan.Service.Instance.middle
{
    [Inject_]
    public class Article_Service : Base_Service, I_Article_Service
    {
        private readonly Product_IOC _product_IOC;

        public Article_Service(Product_IOC product_IOC)
        {
            _product_IOC = product_IOC;
        }
        /// <summary>
        /// 增加文章浏览量
        /// </summary>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public async Task<Api_Result_Dto> Article_LookCount(string articleId)
        {
            // 根据文章 ID 查询文章
            var article = await _product_IOC._productArticle_EFCore.GetAll(a => a.PaId == articleId)
                .FirstOrDefaultAsync();

            if (article != null)
            {

                article.PaLookCount++;
                _product_IOC._productArticle_EFCore.Update(article);
                await _product_IOC._productArticle_EFCore.SaveChangesAsync();
                return Get_Result(0);
            }
            else
            {
                return Get_Result(-1, "未找到的文章");
            }
        }
        /// <summary>
        /// 获取所有文章数据
        /// </summary>
        /// <returns></returns>
        public async Task<Api_Result_Dto> Get_AllArticles()
        {
            var articles = await _product_IOC._productArticle_EFCore
                .GetAll().Where(d => d.PaStatus == 1)
                .ToListAsync();

            if (!articles.Any())
            {
                return Get_Result(0, "查询成功", new List<Article_Response_Dto>());
            }


            var articlesResponse = articles.Select(a =>
            {

                return new Article_Response_Dto
                {
                    id = a.PaId,
                    title = a.PaTitle,
                    cover = a.PaCover,
                    productName = a.PaProductName,
                    productType = a.PaProductType,
                    content = a.PaContent,
                    publishTime = a.PaPublishTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? "",
                    lookCount = a.PaLookCount ?? 0,
                    status = a.PaStatus ?? 0,
                    createTime = a.PaCreateTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? ""
                };
            }).ToList();

            return Get_Result(0, "查询成功", articlesResponse);
        }
        /// <summary>
        /// 获取文章详情
        /// </summary>
        /// <param name="articleId">文章ID</param>
        /// <returns>包含操作结果的DTO</returns>
        public async Task<Api_Result_Dto> Get_ArticleDetails(string articleId)
        {
            try
            {
                // 1. 验证文章ID是否为空
                if (string.IsNullOrEmpty(articleId))
                {
                    return Get_Result(-1, "文章ID不能为空");
                }

                // 2. 根据文章ID查询文章
                var article = await _product_IOC._productArticle_EFCore.GetAll()
                    .FirstOrDefaultAsync(a => a.PaId == articleId);

                if (article == null)
                {
                    return Get_Result(0, "未找到对应的文章记录");
                }

                var articleDto = new Article_Response_Dto
                {
                    id = article.PaId,
                    title = article.PaTitle ?? "无标题",
                    cover = article.PaCover,
                    productName = article.PaProductName ?? "无产品名称",
                    productType = article.PaProductType ?? "无类别",
                    content = article.PaContent ?? "无内容",
                    publishTime = article.PaPublishTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? "未知发布日期",
                    lookCount = article.PaLookCount ?? 0,
                    status = article.PaStatus,
                    createTime = article.PaCreateTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? "未知创建时间"
                };

                return Get_Result(0, "查询成功", articleDto);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"查询文章时发生异常：{ex.ToString()}");

                return Get_Result(-1, "操作失败，请稍后重试");
            }
        }
    }
}
